<!--
  Copyright JS Foundation and other contributors, http://js.foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<script type="text/html" data-template-name="tcp in">
    <div class="form-row">
        <label for="node-input-server"><i class="fa fa-dot-circle-o"></i> <span data-i18n="tcpin.label.type"></span></label>
        <select id="node-input-server" style="width:120px; margin-right:5px;">
            <option value="server" data-i18n="tcpin.type.listen"></option>
            <option value="client" data-i18n="tcpin.type.connect"></option>
        </select>
        <span data-i18n="tcpin.label.port"></span> <input type="text" id="node-input-port" style="width:65px">
    </div>
    <div class="form-row hidden" id="node-input-host-row" style="padding-left:110px;">
        <span data-i18n="tcpin.label.host"></span> <input type="text" id="node-input-host" placeholder="localhost" style="width: 60%;">
    </div>
    <div class="form-row" id="node-input-tls-enable">
        <label> </label>
        <input type="checkbox" id="node-input-usetls" style="display: inline-block; width:auto; vertical-align:top;">
        <label for="node-input-usetls" style="width:auto" data-i18n="httpin.use-tls"></label>
        <div id="node-row-tls" class="hide">
            <label style="width:auto; margin-left:20px; margin-right:10px;" for="node-input-tls"><span data-i18n="httpin.tls-config"></span></label><input type="text" style="width: 300px" id="node-input-tls">
        </div>
    </div>

    <div class="form-row">
        <label><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.output"></span></label>
        <select id="node-input-datamode" style="width:110px;">
            <option value="stream" data-i18n="tcpin.output.stream"></option>
            <option value="single" data-i18n="tcpin.output.single"></option>
        </select>
        <select id="node-input-datatype" style="width:140px;">
            <option value="buffer" data-i18n="tcpin.output.buffer"></option>
            <option value="utf8" data-i18n="tcpin.output.string"></option>
            <option value="base64" data-i18n="tcpin.output.base64"></option>
        </select>
        <span data-i18n="tcpin.label.payload"></span>
    </div>

    <div id="node-row-newline" class="form-row hidden" style="padding-left:110px;">
        <span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;" data-i18n="[placeholder]tcpin.label.optional"><br/>
        <input type="checkbox" id="node-input-trim" style="display:inline-block; width:auto; vertical-align:top;"> <span data-i18n="tcpin.label.reattach"></span>
    </div>

    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> <span data-i18n="common.label.topic"></span></label>
        <input type="text" id="node-input-topic" data-i18n="[placeholder]common.label.topic">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
    </div>
</script>

<script type="text/javascript">
    RED.nodes.registerType('tcp in',{
        category: 'network',
        color: "Silver",
        defaults: {
            name: {value:""},
            server: {value:"server", required:true},
            host: {
                value:"",
                validate:function(v, opt) {
                    if ((this.server == "server")||v.length > 0) {
                        return true;
                    }
                    return RED._("node-red:tcpin.errors.invalid-host");
                }
            },
            port: {
                value:"", required:true,
                label:RED._("node-red:tcpin.label.port"),
                validate:RED.validators.number(false)},
            datamode:{value:"stream"},
            datatype:{value:"buffer"},
            newline:{value:""},
            topic: {value:""},
            trim: {value:false},
            base64: {/*deprecated*/ value:false, required:true},
            tls: {type:"tls-config", value:'', required:false,
                  label:RED._("node-red:httpin.tls-config") }
        },
        inputs:0,
        outputs:1,
        icon: "bridge-dash.svg",
        label: function() {
            return this.name || "tcp:"+(this.host?this.host+":":"")+this.port;
        },
        labelStyle: function() {
            return this.name ? "node_label_italic" : "";
        },
        oneditprepare: function() {
            var updateOptions = function() {
                var sockettype = $("#node-input-server").val();
                if (sockettype == "client") {
                    $("#node-input-host-row").show();
                } else {
                    $("#node-input-host-row").hide();
                }
                var datamode = $("#node-input-datamode").val();
                var datatype = $("#node-input-datatype").val();
                if (datamode == "stream") {
                    if (datatype == "utf8") {
                        $("#node-row-newline").show();
                    } else {
                        $("#node-row-newline").hide();
                    }
                } else {
                    $("#node-row-newline").hide();
                }
            };
            updateOptions();
            $("#node-input-server").change(updateOptions);
            $("#node-input-datatype").change(updateOptions);
            $("#node-input-datamode").change(updateOptions);
            function updateTLSOptions() {
                if ($("#node-input-usetls").is(':checked')) {
                    $("#node-row-tls").show();
                } else {
                    $("#node-row-tls").hide();
                }
            }
            if (this.tls) {
                $('#node-input-usetls').prop('checked', true);
            } else {
                $('#node-input-usetls').prop('checked', false);
            }
            updateTLSOptions();
            $("#node-input-usetls").on("click",function() {
                updateTLSOptions();
            });
        },
        oneditsave: function() {
            if (!$("#node-input-usetls").is(':checked')) {
                $("#node-input-tls").val("_ADD_");
            }
        }
    });
</script>


<script type="text/html" data-template-name="tcp out">
    <div class="form-row">
        <label for="node-input-beserver"><i class="fa fa-dot-circle-o"></i> <span data-i18n="tcpin.label.type"></span></label>
        <select id="node-input-beserver" style="width:150px; margin-right:5px;">
            <option value="server" data-i18n="tcpin.type.listen"></option>
            <option value="client" data-i18n="tcpin.type.connect"></option>
            <option value="reply" data-i18n="tcpin.type.reply"></option>
        </select>
        <span id="node-input-port-row"><span data-i18n="tcpin.label.port"></span> <input type="text" id="node-input-port" style="width: 65px"></span>
    </div>

    <div class="form-row hidden" id="node-input-host-row" style="padding-left: 110px;">
        <span data-i18n="tcpin.label.host"></span> <input type="text" id="node-input-host" style="width: 60%;">
    </div>

    <div class="form-row" id="node-input-tls-enable">
        <label> </label>
        <input type="checkbox" id="node-input-usetls" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-usetls" style="width: auto" data-i18n="httpin.use-tls"></label>
        <div id="node-row-tls" class="hide">
            <label style="width: auto; margin-left: 20px; margin-right: 10px;" for="node-input-tls"><span data-i18n="httpin.tls-config"></span></label><input type="text" style="width: 300px" id="node-input-tls">
        </div>
    </div>

    <div class="form-row hidden" id="node-input-end-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-end" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-end" style="width: 70%;"><span data-i18n="tcpin.label.close-connection"></span></label>
    </div>

    <div class="form-row">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-base64" placeholder="base64" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-base64" style="width: 70%;"><span data-i18n="tcpin.label.decode-base64"></span></label>
    </div>

    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
    </div>
</script>

<script type="text/javascript">
    RED.nodes.registerType('tcp out',{
        category: 'network',
        color: "Silver",
        defaults: {
            name: {value:""},
            host: {
                value:"",
                validate:function(v, opt) {
                    if ((this.beserver != "client")||v.length > 0) {
                        return true;
                    }
                    return RED._("node-red:tcpin.errors.invalid-host");
                }
            },
            port: {
                value:"",
                validate:function(v) {
                    if ((this.beserver == "reply")||RED.validators.number()(v)) {
                        return true;
                    }
                    return RED._("node-red:tcpin.errors.invalid-port");
                }
            },
            beserver: {value:"client", required:true},
            base64: {value:false, required:true},
            end: {value:false, required:true},
            tls: {type:"tls-config", value:'', required:false,
                  label:RED._("node-red:httpin.tls-config") }
        },
        inputs:1,
        outputs:0,
        icon: "bridge-dash.svg",
        align: "right",
        label: function() {
            return this.name || "tcp:"+(this.host?this.host+":":"")+this.port;
        },
        labelStyle: function() {
            return (this.name)?"node_label_italic":"";
        },
        oneditprepare: function() {
            var updateOptions = function() {
                var sockettype = $("#node-input-beserver").val();
                if (sockettype == "reply") {
                    $("#node-input-port-row").hide();
                    $("#node-input-host-row").hide();
                    $("#node-input-end-row").hide();
                    $("#node-input-tls-enable").hide();
                } else if (sockettype == "client"){
                    $("#node-input-port-row").show();
                    $("#node-input-host-row").show();
                    $("#node-input-end-row").show();
                    $("#node-input-tls-enable").show();
                } else {
                    $("#node-input-port-row").show();
                    $("#node-input-host-row").hide();
                    $("#node-input-end-row").show();
                    $("#node-input-tls-enable").show();
                }
            };
            updateOptions();
            $("#node-input-beserver").change(updateOptions);
            function updateTLSOptions() {
                if ($("#node-input-usetls").is(':checked')) {
                    $("#node-row-tls").show();
                } else {
                    $("#node-row-tls").hide();
                }
            }
            if (this.tls) {
                $('#node-input-usetls').prop('checked', true);
            } else {
                $('#node-input-usetls').prop('checked', false);
            }
            updateTLSOptions();
            $("#node-input-usetls").on("click",function() {
                updateTLSOptions();
            });
        },
        oneditsave: function() {
            if (!$("#node-input-usetls").is(':checked')) {
                $("#node-input-tls").val("_ADD_");
            }
        }
    });
</script>


<script type="text/html" data-template-name="tcp request">
    <div class="form-row">
        <label for="node-input-server"><i class="fa fa-globe"></i> <span data-i18n="tcpin.label.server"></span></label>
        <input type="text" id="node-input-server" placeholder="ip.address" style="width:45%">
        <span data-i18n="tcpin.label.port"></span>
        <input type="text" id="node-input-port" style="width:60px">
    </div>
    <div class="form-row" id="node-input-tls-enable">
        <label> </label>
        <input type="checkbox" id="node-input-usetls" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-usetls" style="width: auto" data-i18n="httpin.use-tls"></label>
        <div id="node-row-tls" class="hide">
            <label style="width: auto; margin-left: 20px; margin-right: 10px;" for="node-input-tls"><span data-i18n="httpin.tls-config"></span></label><input type="text" style="width: 300px" id="node-input-tls">
        </div>
    </div>
    <div class="form-row">
        <label for="node-input-ret"><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.return"></span></label>
        <select type="text" id="node-input-ret" style="width:54%;">
            <option value="buffer" data-i18n="tcpin.output.buffer"></option>
            <option value="string" data-i18n="tcpin.output.string"></option>
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-out"><i class="fa fa-sign-out fa-rotate-90"></i> <span data-i18n="tcpin.label.close"></span></label>
        <select type="text" id="node-input-out" style="width:54%;">
            <option value="time" data-i18n="tcpin.return.timeout"></option>
            <option value="char" data-i18n="tcpin.return.character"></option>
            <option value="count" data-i18n="tcpin.return.number"></option>
            <option value="sit" data-i18n="tcpin.return.never"></option>
            <option value="immed" data-i18n="tcpin.return.immed"></option>
        </select>
        <input type="text" id="node-input-splitc" style="width:50px;">
        <span id="node-units"></span>
    </div>
    <div id="node-row-newline" class="form-row hidden" style="padding-left:162px;">
        <span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;" data-i18n="[placeholder]tcpin.label.optional"><br/>
        <input type="checkbox" id="node-input-trim" style="display:inline-block; width:auto; vertical-align:top;"> <span data-i18n="tcpin.label.reattach"></span>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
    </div>
</script>

<script type="text/javascript">
    RED.nodes.registerType('tcp request',{
        category: 'network',
        color: "Silver",
        defaults: {
            name: {value:""},
            server: {value:""},
            port: {
                value:"",
                label: RED._("node-red:tcpin.label.port"),
                validate:RED.validators.regex(/^(\d*|)$/)
            },
            out: {value:"time", required:true},
            ret: {value:"buffer"},
            splitc: {value:"0", required:true},
            newline: {value:""},
            trim: {value:false},
            tls: {type:"tls-config", value:'', required:false, label:RED._("node-red:httpin.tls-config")}
        },
        inputs:1,
        outputs:1,
        icon: "bridge-dash.svg",
        label: function() {
            return this.name || "tcp:"+(this.server?this.server+":":"")+this.port;
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            var previous = null;
            if ($("#node-input-ret").val() == undefined) {
                $("#node-input-ret").val("buffer");
                this.ret = "buffer";
            }
            $("#node-input-ret").on("change", function() {
                if ($("#node-input-ret").val() === "string" && $("#node-input-out").val() === "sit") { $("#node-row-newline").show(); }
                else { $("#node-row-newline").hide(); }
            });
            $("#node-input-out").on("change", function() {
                if ($("#node-input-ret").val() === "string" && $("#node-input-out").val() === "sit") { $("#node-row-newline").show(); }
                else { $("#node-row-newline").hide(); }
            });
            $("#node-input-out").on('focus', function () { previous = this.value; }).on("change", function() {
                $("#node-input-splitc").show();
                if (previous === null) { previous = $("#node-input-out").val(); }
                if ($("#node-input-out").val() == "char") {
                    if (previous != "char") { $("#node-input-splitc").val("\\n"); }
                    $("#node-units").text("");
                }
                else if ($("#node-input-out").val() == "time") {
                    if (previous != "time") { $("#node-input-splitc").val("0"); }
                    $("#node-units").text(RED._("node-red:tcpin.label.ms"));
                }
                else if ($("#node-input-out").val() == "immed") {
                    if (previous != "immed") { $("#node-input-splitc").val(" "); }
                    $("#node-units").text("");
                    $("#node-input-splitc").hide();
                }
                else if ($("#node-input-out").val() == "count") {
                    if (previous != "count") { $("#node-input-splitc").val("12"); }
                    $("#node-units").text(RED._("node-red:tcpin.label.chars"));
                }
                else {
                    if (previous != "sit") { $("#node-input-splitc").val(" "); }
                    $("#node-units").text("");
                    $("#node-input-splitc").hide();
                }
            });
            function updateTLSOptions() {
                if ($("#node-input-usetls").is(':checked')) {
                    $("#node-row-tls").show();
                } else {
                    $("#node-row-tls").hide();
                }
            }
            if (this.tls) {
                $('#node-input-usetls').prop('checked', true);
            } else {
                $('#node-input-usetls').prop('checked', false);
            }
            updateTLSOptions();
            $("#node-input-usetls").on("click",function() {
                updateTLSOptions();
            });
        },
        oneditsave: function() {
            if (!$("#node-input-usetls").is(':checked')) {
                $("#node-input-tls").val("_ADD_");
            }
        }
    });
</script>
